﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for BrokerContactDB
/// </summary>
namespace CE.User
{
    public class BrokerContactDB
    {
        public BrokerContactDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(BrokerContact brokercontact)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerContactInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ContactId", brokercontact.ContactId);
            _sPara.Add("@Email", brokercontact.Email);
            _sPara.Add("@Mobile", brokercontact.Mobile == string.Empty ? string.Empty : brokercontact.Mobile);
            _sPara.Add("@HomePostCode", brokercontact.HomePostCode == string.Empty ? string.Empty : brokercontact.HomePostCode);
            _sPara.Add("@OfficePostCode", brokercontact.OfficePostCode == string.Empty ? string.Empty : brokercontact.OfficePostCode);
            _sPara.Add("@HomeUnitNo", brokercontact.HomeUnitNo);
            _sPara.Add("@OfficeUnitNo", brokercontact.OfficeUnitNo);
            _sPara.Add("@HomeLevelNo", brokercontact.HomeLevelNo);
            _sPara.Add("@OfficeLevelNo", brokercontact.OfficeLevelNo);
            _sPara.Add("@HomeStreet", brokercontact.HomeStreet);
            _sPara.Add("@OfficeStreet", brokercontact.OfficeStreet);
            _sPara.Add("@HomeFax", brokercontact.HomeFax);
            _sPara.Add("@OfficeFax", brokercontact.OfficeFax);
            _sPara.Add("@HomePhone", brokercontact.HomePhone == string.Empty ? string.Empty : brokercontact.HomePhone);
            _sPara.Add("@OfficePhone", brokercontact.OfficePhone == string.Empty ? string.Empty : brokercontact.OfficePhone);
            _sPara.Add("@HomeCity", brokercontact.HomeCity);
            _sPara.Add("@HomeCountry", brokercontact.HomeCountry);
            _sPara.Add("@OfficeCity", brokercontact.OfficeCity);
            _sPara.Add("@OfficeCountry", brokercontact.OfficeCountry);
            _sPara.Add("@Status", brokercontact.Status);
            _sPara.Add("@OtherContact1", brokercontact.OtherContact1);
            _sPara.Add("@OtherContact2", brokercontact.OtherContact2);
            _sPara.Add("@RefCEUserId", brokercontact.RefCEUserId);
            _sPara.Add("@Info1", brokercontact.Info1);
            _sPara.Add("@Info2", brokercontact.Info2);
            _sPara.Add("@Info3", brokercontact.Info3);
            _sPara.Add("@Info4", brokercontact.Info4);
            _sPara.Add("@Info5", brokercontact.Info5);
            _sPara.Add("@Info6", brokercontact.Info6);
            _sPara.Add("@Info7", brokercontact.Info7);
            _sPara.Add("@Info8", brokercontact.Info8);
            _sPara.Add("@Info9", brokercontact.Info9);
            _sPara.Add("@Info10", brokercontact.Info10);
            _sPara.Add("@Info11", brokercontact.Info11);
            _sPara.Add("@Info12", brokercontact.Info12);
            _sPara.Add("@Info13", brokercontact.Info13);
            _sPara.Add("@Info14", brokercontact.Info14);
            _sPara.Add("@Info15", brokercontact.Info15);
            _sPara.Add("@Info16", brokercontact.Info16);
            _sPara.Add("@Info17", brokercontact.Info17);
            _sPara.Add("@Info18", brokercontact.Info18);
            _sPara.Add("@Info19", brokercontact.Info19);
            _sPara.Add("@Info20", brokercontact.Info20);
            _sPara.Add("@Einfo2", brokercontact.Einfo2);
            _sPara.Add("@Einfo4", brokercontact.Einfo4);
            _sPara.Add("@Einfo5", brokercontact.Einfo5);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public BrokerContact GetContact(Int64 RefCEUserId)
        {
            MyDatabase _Db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocBrokerContactSelectSingleItem";
            BrokerContact _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new BrokerContact();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private BrokerContact FillDataRecord(DataTable myDataTable)
        {
            BrokerContact myContact = new BrokerContact();
            myContact.ContactId = Convert.ToInt64(myDataTable.Rows[0]["ContactId"].ToString());
            myContact.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());

            string _email = myDataTable.Rows[0]["Email"].ToString();
            if (_email != string.Empty)
            {
                myContact.Email = _email;
            }
            string _mobile = myDataTable.Rows[0]["Mobile"].ToString();
            if (_mobile != string.Empty)
            {
                myContact.Mobile = _mobile;
            }
            string _homephone = myDataTable.Rows[0]["HomePhone"].ToString();
            if (_homephone != string.Empty)
            {
                myContact.HomePhone = _homephone;
            }
            myContact.HomeFax = myDataTable.Rows[0]["HomeFax"].ToString();
            myContact.HomeUnitNo = myDataTable.Rows[0]["HomeUnitNo"].ToString();
            myContact.HomeLevelNo = myDataTable.Rows[0]["HomeLevelNo"].ToString();
            myContact.HomeStreet = myDataTable.Rows[0]["HomeStreet"].ToString();
            myContact.HomeCity = myDataTable.Rows[0]["HomeCity"].ToString();
            myContact.HomeCountry = myDataTable.Rows[0]["HomeCountry"].ToString();
            string _homepostcode = myDataTable.Rows[0]["HomePostCode"].ToString();
            if (_homepostcode != string.Empty)
            {
                myContact.HomePostCode = _homepostcode;
            }
            myContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeUnitNo"].ToString();
            myContact.OfficeUnitNo = myDataTable.Rows[0]["OfficeLevelNo"].ToString();
            string _officephone = myDataTable.Rows[0]["OfficePhone"].ToString();
            if (_officephone != string.Empty)
            {
                myContact.OfficePhone = _officephone;
            }
            myContact.OfficeFax = myDataTable.Rows[0]["OfficeFax"].ToString();
            myContact.OfficeStreet = myDataTable.Rows[0]["OfficeStreet"].ToString();
            myContact.OfficeCity = myDataTable.Rows[0]["OfficeCity"].ToString();
            myContact.OfficeCountry = myDataTable.Rows[0]["OfficeCountry"].ToString();
            string _officepostcode = myDataTable.Rows[0]["OfficePostCode"].ToString();
            if (_officepostcode != string.Empty)
            {
                myContact.OfficePostCode = _officepostcode;
            }
            myContact.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myContact.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myContact.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myContact.Info4 = myDataTable.Rows[0]["Info4"].ToString();
            myContact.Einfo1 = myDataTable.Rows[0]["Einfo1"].ToString();
            myContact.Einfo2 = myDataTable.Rows[0]["Einfo2"].ToString();
            myContact.Einfo3 = myDataTable.Rows[0]["Einfo3"].ToString();
            myContact.Einfo4 = myDataTable.Rows[0]["Einfo4"].ToString();
            myContact.Einfo5 = myDataTable.Rows[0]["Einfo5"].ToString();
            return myContact;
        }

    }
}